package aop;

import dao.LogSaverImpl;
import domain.User;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.SQLException;

@Component
@Aspect
public class LogAdvise {

    @Autowired
    private LogSaverImpl logSaver;

    public Integer id=null;


    //切入点的配置（登录）
    @Pointcut("execution(* service.UserServiceImpl.login(..))")
    public void pointcut(){}

    //监控register
    @Pointcut("execution(* service.UserServiceImpl.regist(..))")
    public void regist(){}

    /*    //用户登录
    @Pointcut("execution(* controller.UserController.*(..))")
    public void logInCall() {

    }*/

/*    @Around("pointcut()")
    public Object aroundLog(ProceedingJoinPoint joinPoint) throws Throwable {

        Signature signature = joinPoint.getSignature();
        logger.info("当前类是： "+signature.getDeclaringTypeName()+",执行方法是： "+signature.getName());

        Object proceed = joinPoint.proceed();
        return proceed;
    }*/

     @AfterReturning(value="regist()",argNames = "joinPoint,object", returning = "object")
     public void doAfter(JoinPoint joinPoint,Object object) throws SQLException {
         User user = (User) object;
         if(user == null){
             return;
         }
         if(joinPoint.getArgs() == null){
             return;
         }
         id = user.getId();
         String methodName = joinPoint.getSignature().getName();

         Log log = new Log();
         log.setUserId(id);
         log.setUserName(user.getUsername());
         logSaver.logsaver(log);

         System.out.println(log);

         System.out.println("=====SysLogAspect后置通知开始=====");
     }


}
